<?php

/**
 *  http 优化基础类库
 * Class Http
 */
class Http{
    CONST HOST = '0.0.0.0';
    CONST PORT = 6061;
    public $http = null;
    public function __construct()
    {
        $this->http = new swoole_http_server(self::HOST,self::PORT);
        //设置访问静态文件
        $this->http->set([
            'enable_static_handler' => true,
            'document_root' => '/wwwdata/tp5/public/static', // v4.4.0以下版本, 此处必须为绝对路径
            'worker_num'=>4,
            'task_worker_num'=>4
        ]);
        $this->http->on("WorkerStart",[$this,"onWorkerStart"]);
        $this->http->on("request",[$this,"onRequest"]);

        $this->http->on("task",[$this,"onTask"]);
        $this->http->on("finish",[$this,"onFinish"]);
        //关闭事件
        $this->http->on("close",[$this,"onClose"]);
        //启动
        $this->http->start();
    }

    /**
     * request 回调
     * @param $request
     * @param $response
     */
    public function onRequest($request,$response)
    {
        $_SERVER = [];
        if(isset($request->server)){
            foreach ($request->server as $k=>$v) {
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        if(isset($request->header)){
            foreach ($request->header as $k=>$v) {
                $_SERVER[strtoupper($k)] = $v;
            }
        }
        $_GET = [];
        if(isset($request->get)){
            foreach ($request->get as $k=>$v) {
                $_GET[$k] = $v;
            }
        }
        $_POST = [];
        if(isset($request->post)){
            foreach ($request->post as $k=>$v) {
                $_POST[$k] = $v;
            }
        }
        $_POST['http_server'] = $this->http;
        ob_start();
        try {
            think\Container::get('app')->run()->send();

        }catch (Exception $e)
        {
            //todo
        }
        $res = ob_get_contents();
        ob_clean();
        $response->end($res);
    }
    /**
     * task 异步请求
     * @param $http
     * @param $task_id
     * @param $src_tack_id
     * @param $data
     * @return string
     */
    public function onTask($http,$task_id,$src_tack_id,$data)
    {
        //分发task机制 让不同的任务 走不同的逻辑
        $obj = new app\common\lib\task\Task;
        $method =  $data['method'];
        $flag = $obj->$method($data['data']);
        return $flag;


        $http->push($data['fd'],"server-push:".date('Y-m-d H:i:S')."10秒后 啦啦啦啦");
        return "on task finish";//告诉worker
    }

    /**
     * @param $http
     * @param $task_id
     * @param $data
     * @return string
     */
    public function onFinish($http,$task_id, $data)
    {
        $str = "task_id:{$task_id}\n";
        $str .=  "finish-success:{$data}\n";
        return $str;

    }
    /**
     *workerStart 回调
     * @param $http
     * @param $worker_id
     */
    public function onWorkerStart($http,$worker_id)
    {
        define('APP_PATH',__DIR__."/../application");
        require __DIR__ . '/../public/index.php';
    }

    /**
     * 关闭
     * @param $http
     * @param $fd
     */
    public function onClose($http,$fd)
    {
        echo "client_id:{$fd}\n";
    }
}
 new Http();